﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Web_index : System.Web.UI.Page
{
    protected List<Train> ts;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            CmsDataContext db = new CmsDataContext();
            //
            //图片新闻
            //
            string pic_id = GetTopicId("图片新闻");
            DataList1.DataSource = (from aA in db.Article
                                    where aA.TopicID.Equals(pic_id) && aA.Verified != null
                                    orderby aA.Published descending
                                    select aA).Take(5);
            DataList1.DataBind();
            //
            //信息联播
            //
            setInit(Repeater1, GetTopicId("信息联播"), 7);
            //
            //通知公告
            //
            setInit(Repeater2, GetTopicId("通知公告"), 7);
            

            //
            //政策法规
            //
            setInit(Repeater4, GetTopicId("政策法规"), 6);
            //
            //技术推广
            //
            setInit(Repeater5, GetTopicId("技术推广"), 6);
            //
            //新产品推广
            //
            setInit(Repeater6, GetTopicId("新产品推广"), 6);
            //
            //企业招聘
            //
            //setInit(Repeater7, GetTopicId("企业招聘"), 6);
            //
            //教师风采
            //
            setInit(Repeater8, GetTopicId("教师风采"), 6);
            //
            //学员风采
            //
            setInit(Repeater9, GetTopicId("学员风采"), 6);
            //
            //典型阵地
            //
            setInit(Repeater10, GetTopicId("示范阵地"), 6);
            //
            //图片新闻
            //
            string pic2_id = GetTopicId("培训回顾");
            var pic2 = (from aA in db.Article 
                       where aA.TopicID.Equals(pic2_id) && aA.Verified != null 
                       orderby aA.Published descending
                       select aA).Take(10);
            List<Article> lpic2 = new List<Article>();
            foreach (Article aP in pic2)
            {
                if (GetImageUrl(aP.Body.ToString()) != "" && GetImageUrl(aP.Body.ToString()) != null)
                {
                    lpic2.Add(aP);
                }
            }
            DataList2.DataSource = lpic2;
            DataList2.DataBind();


            //
            //领导讲话
            //
            setInit(Repeater11, GetTopicId("领导讲话"), 6);
            //
            //制度文件
            //
            setInit(Repeater12, GetTopicId("制度文件"), 6);
            //
            //他山之石
            //
            setInit(Repeater13, GetTopicId("他山之石"), 6);
            //
            //在线答疑
            //
            List<Question> lq = new List<Question>();
            var q = from aQ in db.Question 
                    where aQ.Country.Equals("东营区") && aQ.Verified != null 
                    orderby aQ.Time descending 
                    select aQ;
            foreach (Question aq in q)
            {
                var aaq = from aA in db.Answer where aA.QuestionID.Equals(aq.ID) select aA;
                if (aaq.Count() != 0)
                {
                    lq.Add(aq);
                }
                if (lq.Count() >= 4)
                {
                    break;
                }
            }
            Repeater14.DataSource = lq;
            Repeater14.DataBind();

            var q2 = from aQ in db.Question 
                     where aQ.Country.Equals("河口区") && aQ.Verified != null 
                     orderby aQ.Time descending 
                     select aQ;
            lq.Clear();
            foreach (Question aq in q2)
            {
                var aaq = from aA in db.Answer where aA.QuestionID.Equals(aq.ID) select aA;
                if (aaq.Count() != 0)
                {
                    lq.Add(aq);
                }
                if (lq.Count() >= 4)
                {
                    break;
                }
            }
            Repeater15.DataSource = lq;
            Repeater15.DataBind();
            var q3 = from aQ in db.Question 
                     where aQ.Country.Equals("广饶县") && aQ.Verified != null 
                     orderby aQ.Time descending 
                     select aQ;
            lq.Clear();
            foreach (Question aq in q3)
            {
                var aaq = from aA in db.Answer where aA.QuestionID.Equals(aq.ID) select aA;
                if (aaq.Count() != 0)
                {
                    lq.Add(aq);
                }
                if (lq.Count() >= 4)
                {
                    break;
                }
            }
            Repeater16.DataSource = lq;
            Repeater16.DataBind();

            var q4 = from aQ in db.Question 
                     where aQ.Country.Equals("垦利县") && aQ.Verified != null
                     orderby aQ.Time descending 
                     select aQ;
            lq.Clear();
            foreach (Question aq in q4)
            {
                var aaq = from aA in db.Answer where aA.QuestionID.Equals(aq.ID) select aA;
                if (aaq.Count() != 0)
                {
                    lq.Add(aq);
                }
                if (lq.Count() >= 4)
                {
                    break;
                }
            }
            Repeater17.DataSource = lq;
            Repeater17.DataBind();

            var q5 = from aQ in db.Question
                     where aQ.Country.Equals("利津县") && aQ.Verified != null 
                     orderby aQ.Time descending 
                     select aQ;
            lq.Clear();
            foreach (Question aq in q5)
            {
                var aaq = from aA in db.Answer where aA.QuestionID.Equals(aq.ID) select aA;
                if (aaq.Count() != 0)
                {
                    lq.Add(aq);
                }
                if (lq.Count() >= 4)
                {
                    break;
                }
            }
            Repeater18.DataSource = lq;
            Repeater18.DataBind();


            #region 示范阵地地图
            string aid = Toolkit.ArticleIdOfFrontShizhi;
            var qq = from aA in db.Article where aA.ID == aid select aA;
            if (qq.Count() > 0)
            {
                Article article = qq.First();
                fntsz.Attributes.Add("href", string.Format("../Html/{0}", article.Url));
            }

            aid = Toolkit.ArticleIdOfFrontDongyingqu;
            qq = from aA in db.Article where aA.ID == aid select aA;
            if (qq.Count() > 0)
            {
                Article article = qq.First();
                fntdyq.Attributes.Add("href", string.Format("../Html/{0}", article.Url));
            }


            aid = Toolkit.ArticleIdOfFrontHekouqu;
            qq = from aA in db.Article where aA.ID == aid select aA;
            if (qq.Count() > 0)
            {
                Article article = qq.First();
                fnthkq.Attributes.Add("href", string.Format("../Html/{0}", article.Url));
            }

            aid = Toolkit.ArticleIdOfFrontGuangraoxian;
            qq = from aA in db.Article where aA.ID == aid select aA;
            if (qq.Count() > 0)
            {
                Article article = qq.First();
                fntgrx.Attributes.Add("href", string.Format("../Html/{0}", article.Url));
            }

            aid = Toolkit.ArticleIdOfFrontKenlixian;
            qq = from aA in db.Article where aA.ID == aid select aA;
            if (qq.Count() > 0)
            {
                Article article = qq.First();
                fntklx.Attributes.Add("href", string.Format("../Html/{0}", article.Url));
            }

            aid = Toolkit.ArticleIdOfFrontLijinxian;
            qq = from aA in db.Article where aA.ID == aid select aA;
            if (qq.Count() > 0)
            {
                Article article = qq.First();
                fntljx.Attributes.Add("href", string.Format("../Html/{0}", article.Url));
            }
            #endregion
        }
    }
    protected Answer GetAnswer(string quest_id)
    { 
        CmsDataContext db=new CmsDataContext();
        var a = from aA in db.Answer where aA.QuestionID.Equals(quest_id) select aA;
        return a.First();
    }
    protected string GetImageUrl(string content)
    {
        Regex regex = new Regex(@"<IMG[^>]+src=\s*(?:'(?<src>[^']+)'|""(?<src>[^""]+)""|(?<src>[^>\s]+))\s*[^>]*>", RegexOptions.IgnoreCase);
        MatchCollection mc = regex.Matches(content);
        if (mc.Count != 0)
        {
            return mc[0].Groups["src"].Value.ToLower();
        }
        else
        {
            return "";
        }

    }
    protected string GetTopicId(string name)
    {
        CmsDataContext db = new CmsDataContext();
        string id = "";
        try
        {
            id = (from aT in db.Topic where aT.Name.Equals(name) select aT).First().ID;
            
        }
        catch (Exception e)
        { 
            
        }
        return id;
    }
    protected Article GetArticle(string id)
    { 
        CmsDataContext db=new CmsDataContext();
        var a = from aA in db.Article where aA.TopicID.Equals(id) && aA.Verified != null select aA;
        if (a.Count() != 0)
        {
            return a.First();
            
        }
        else
        {
            return null;
        }
    }
    private void setInit(Repeater r, string topic_id, int size)
    {
        CmsDataContext db=new CmsDataContext();
        var a = (from aA in db.Article
                 where aA.TopicID.Equals(topic_id) && aA.Verified != null 
                 orderby aA.Published descending 
                 select aA).Take(size);
        r.DataSource = a;
        r.DataBind();
    }
    protected static string stripHtml(string strHtml)
    {
        Regex objRegExp = new Regex("<(.|\n)+?>");
        //return  Regex.Replace(strHtml, "<[^>]*>", "");
        string strOutput = objRegExp.Replace(strHtml, "");
        strOutput = strOutput.Replace("<", "");
        strOutput = strOutput.Replace(">", "");
        strOutput = strOutput.Replace("&nbsp;", "");
        return strOutput;
    }

       
}